<?php 
require_once "cw2conf.php";
include_once "misc.php";

function ShowMap($tournament_id)
{

?>

<html>
<head><title>Clan War Map</title>

<script type="text/javascript" src="cw2.js"></script>

<script type="text/javascript">
// centers icon positions on load

// Opera specific. Copies Title attribute from parent DIV to child element.
function copyTitle(event)
{
	p = this.parentElement;
	while(p!=null)
	{
		if(p.id=="icon")
		{
			event.srcElement.title = p.title;
			break;
		} // close if()
		p = p.parentElement;
	} // close while()
} // close function

function doOnLoad()
{
 doPosition();
}

</script>

</head>

<style>
div {margin:0}
</style>

<body onload="doOnLoad()" onmouseover="copyTitle(event)" style="margin:0; background-color:06283b">

<?php

 global $dbl;

 global $planet_icon_radius, $planet_icon_type, $fort_icon_radius, $fort_icon_type, $unit_group_icon_radius, $unit_group_icon_type, $min_radius;
 
 $unit_type_texts = array('army' => "(Army)", 'garrison' => "(Garrison)");
 
 global $gotdata, $tournament, $tgroups, $planets, $tplayers,$units,$unit_groups;
 
 if (GetTournament($tournament_id)<0) {return;}
 if (GetTournamentGroups($tournament_id)<0) {return;}
 if (GetTournamentPlayers($tournament_id)<0) {return;}
 if (GetPlanets($tournament_id)<0) {return;}
 if (GetUnits($tournament_id)<0) {return;}
 
 //  TOURNAMENT
 echo '<img src="' . $tournament['map_img_url'] . '">';
 
 //  PLANETS
 if (!(IsSet($gotdata['PlanetIcons'])))
 {
  $gotdata['PlanetIcons'] = 1;
  //print_r($planets);
  foreach ($planets as $planet_id=>$planet)
  {
   $planets[$planet_id]['d_icons'] = array();
   if ($planets[$planet_id]['planet_size'] > 0)
   {
    $planets[$planet_id]['d_icons'][] = array('r' => $planet_icon_radius, 'type' => $planet_icon_type, 'id' => $planet['planet_id']);
   }
   if ($planets[$planet_id]['planet_flags'] & CW2_PLANET_FORT == CW2_PLANET_FORT)
   {
    $planets[$planet_id]['d_icons'][] = array('r' => $fort_icon_radius, 'type' => $fort_icon_type, 'id' => $planet['planet_id']);
   }
  }
 }

 //  UNIT GROUPS
 GetUnitGroups($tournament_id);
 
 foreach ($planets as $planet_id => $planet)
 {
  $num_icons = count($planet['d_icons']);
  $circuit = 0;
  foreach ($planet['d_icons'] as $d_icon)
  {
   $circuit += $d_icon['r'];
   //echo "<br>planet=".$planet['planet_name']."  +r=".$d_icon['r']." sum=$circuit";
  }
  if ($circuit == 0)
  {
   continue;
  }
  
  $radius = $circuit / pi() / 2;
  $circuit_multiplier = 1;
  if ($radius < $min_radius)
  {
   $circuit_multiplier = $min_radius / $radius;
   $circuit = $min_radius * pi() * 2;
   $radius = $min_radius;
  }
  
  $curr_circuit = 0;
  foreach ($planet['d_icons'] as $d_icon_idx => $d_icon)
  {
   //$angle = pi() * 2 * $d_icon_idx / $num_icons; // this one distributes evenly
   if ($d_icon_idx > 0)
   {
    $curr_circuit += $d_icon['r'] / 2;
   }
   //echo "<br>planet=".$planet['planet_name']." d_icon_idx=$d_icon_idx curr_circuit=$curr_circuit circuit_multiplier=$circuit_multiplier";
   $angle = 2 * pi() * ($curr_circuit * $circuit_multiplier) / $circuit; // more space for bigger icons
   $coord_x = round($planet['planet_x'] + $radius * sin ($angle));
   $coord_y = round($planet['planet_y'] - $radius * cos ($angle)); 
   //echo " angle=".round($angle / 2 / pi() * 360)." coord_x=$coord_x coord_y=$coord_y";
   $curr_circuit += $d_icon['r'] / 2;
   
   if ($d_icon['type'] == $unit_group_icon_type)
   {
    $unit_group = $unit_groups[$d_icon['id']];
    
    $tgroup_id = $unit_group['tgroup_id'];
    $tgroup_color = "#" . ColorToHex($tgroups[$tgroup_id]['color']);
    $tgroup_name = $tgroups[$tgroup_id]['name'];
    $tgroup_logo_url = $tgroups[$tgroup_id]['group_logo_url'];

    echo '
	<div id="icon" style="display:none; border:1px solid ' . $tgroup_color . '; color: white; background-color: black; font-family: arial;
			font-size: 8pt; position: absolute; width: 25px; left:' . $coord_x . '; top:' . $coord_y . '" ';
			 echo 'title="' . $tgroup_name . ":
---
";
    foreach ($unit_group['units'] as $unit_id)
    {
     $tplayer_name = $tplayers[$units[$unit_id]['tplayer_id']]['name'];
     if ($units[$unit_id]['unit_flags'] & CW2_UNIT_GARRISON == CW2_UNIT_GARRISON)
     {
      $unit_type_text = $GLOBALS['lang_cw2_unit_type_texts']['garrison'];
     } else {
      $unit_type_text = $GLOBALS['lang_cw2_unit_type_texts']['army'];
     }
     echo $tplayer_name . ' ' . $unit_type_text . '
';
    }
    echo '">';
    
    echo '<span style=" width: 25px; text-align: center"><img src="../' . $tgroup_logo_url . '"></span><br/>';
    echo '<div style="width: 25px; text-align: center; ">' . $unit_group['num_armies'] . '/' . $unit_group['num_garrisons'] . '</div></div>';

   }
   else if ($d_icon['type'] == $planet_icon_type)
   {
    $planet = $planets[$d_icon['id']];
    $tgroup_id = $planet['tgroup_id'];
    
    
    echo '
	<a href="./systembattle.php?tournament_id=' . $tournament_id . '&planet_id=' . $planet['planet_id'];
    echo '" style="color: ' . $planet['tgroup_color'] . '">';
    echo '<div id="icon" style="display:none; position: absolute; left:' . $coord_x . ';	top: ' . $coord_y . ' "title="' . $planet['planet_name'];
    echo ' (' . $planet['map_name'] . ', ' . $planet['planet_size_text'];
    echo ') ' . $planet['tgroup_name'] . '"><nobr>';
    if ($planet['tgroup_logo_url'] != "")
    {
     echo '<img src="../' . $planet['tgroup_logo_url'] . '" style="border:1px solid ' . $planet['tgroup_color'] . '; vertical-align: middle;">';
    }
 		 echo '<span style="border:0px solid ' . $planet['tgroup_color'] . '; color: white; font-family: arial; font-size: 9pt; font-weight: bold;">';
    echo '&nbsp;' . $planet['planet_name'] . '&nbsp;(' . $planet['planet_size_text'] . ')&nbsp;</span></nobr></div></a>';
   }
   else if ($d_icon['type'] == $fort_icon_type)
   {
    echo '
	<div id="icon" style="display:none; position: absolute; left:' . $coord_x . '; top:' . $coord_y . '"title="' . $planet['tgroup_name'] . ' Fort">';
    echo '<img src="../Logo/fort.gif"></div>';
   }
   else
   {
    fecho("Unknown icon type '" . $d_icon['type'] . "' - planet " . $planet['planet_id'] . ", icon idx " . $d_icon_idx);
   }
  }  
 }
}

function EchoClanTD_cl($tgroup_color, $tgroup_logo_url)
{
 echo '<td>';
 if ($tgroup_logo_url != "")
 {
  echo '<center><img src="../' . $tgroup_logo_url . '" style="border:1px solid ' . $tgroup_color . '; vertical-align: middle;"></center>';
 }
 echo '</td>';
}

function EchoUnitTR_id($unit_id)
{
 global $units, $orders, $tplayers, $tgroups;
 $unit = &$units[$unit_id];
 $tplayer_id = $unit['tplayer_id'];
 $tgroup_id = $unit['tgroup_id'];
 if ($unit['unit_flags'] & CW2_UNIT_GARRISON == CW2_UNIT_GARRISON)
 {
  $unit_type_char = $GLOBALS['lang_cw2_unit_type_chars']['garrison'];
 }
 else
 {
  $unit_type_char = $GLOBALS['lang_cw2_unit_type_chars']['army'];
 }
 $tplayer_name = $tplayers[$tplayer_id]['name'];
 $tgroup = &$tgroups[$unit['tgroup_id']];
 $tgroup_color = "#" . ColorToHex($tgroup['color']);
 echo '
 <tr id="unit_'.$unit['js_id'].'" OnClick="unitOrdersClick('.$unit['js_id'].');"><td><center><font color="' . $tgroup_color. '"> ' . $tplayer_name . ' ' . $unit_type_char;
 echo '<br>';
 
 echo '</font></center></td>';
 EchoClanTD_cl($tgroup_color, $tgroup['group_logo_url']);
 echo '</tr>';
}

function EchoFortTR_cl($tgroup_color, $tgroup_logo_url, $planet_jsid)
{
 $txt = $GLOBALS['lang_cw2_fort'];
 echo '
<tr id="fort_'.$planet_jsid.'" OnClick="fortOrdersClick('.$planet_jsid.');"><td><center><font color="'.$tgroup_color.'">'.$txt.'</font></center></td>';
 EchoClanTD_cl($tgroup_color, $tgroup_logo_url);
 echo '</tr>';
}

function ShowKey($tournament_id)
{
 global $dbl;
 global $gotdata, $tournament, $tgroups, $planets, $tplayers, $units, $unit_groups, $orders, $order_types, $turn;
 global $logged_player_id, $logged_tplayer_id, $logged_permissions, $logged_tgroup_id;
 
 if (GetTournament($tournament_id)<0) {return;}
 if (GetTournamentGroups($tournament_id)<0) {return;}
 if (GetPlanets($tournament_id)<0) {return;}
 if (GetTournamentPlayers($tournament_id)<0) {return;}
 if (GetTournamentPlayerRanks($tournament_id)<0) {return;}
 if (GetUnits($tournament_id)<0) {return;}
 if (GetUnitGroups($tournament_id)<0) {return;}
 if (GetOrders($tournament_id, $turn)<0) {return;}
 
 echo '<html><head></head>
 <script type="text/javascript">
  var Tournament_id = '.$tournament_id.';
  var Current_turn = '.$tournament['current_turn'].';
  var CW2_DEBUG = '.CW2_DEBUG.';
  var CW2_NO_SID = '.CW2_NO_SID.';
  var CW2_NO_OBJECT = '.CW2_NO_OBJECT.';
  var CW2_UNIT_GARRISON = '.CW2_UNIT_GARRISON.';
  var CW2_PLANET_FORT = '.CW2_PLANET_FORT.';
  var CW2_PLANET_ONGOING_COMBAT = '.CW2_PLANET_ONGOING_COMBAT.';

  var CW2_CLAN_MEMBER = '.CW2_CLAN_MEMBER.';
  var CW2_CLAN_PLAYER = '.CW2_CLAN_PLAYER.';
  var CW2_CLAN_OWN_UNIT_ORDERS = '.CW2_CLAN_OWN_UNIT_ORDERS.';
  var CW2_CLAN_EVERYONE_UNIT_ORDERS = '.CW2_CLAN_EVERYONE_UNIT_ORDERS.';
  var CW2_CLAN_BUILD_RECRUIT = '.CW2_CLAN_BUILD_RECRUIT.';
  var CW2_CLAN_ADMIN = '.CW2_CLAN_ADMIN.';
  var CW2_MAX_CLAN_PERMISSIONS = '.CW2_MAX_CLAN_PERMISSIONS.';
  var CW2_TOURNAMENT_MODERATOR = '.CW2_TOURNAMENT_MODERATOR.';
  var CW2_MAX_HUMAN_PERMISSIONS = '.CW2_MAX_HUMAN_PERMISSIONS.';

  var CW2_NO_ORDER = '.CW2_NO_ORDER.';
  var CW2_ORDER_ORDER = '.CW2_ORDER_ORDER.';
  var CW2_ORDER_UNIT = '.CW2_ORDER_UNIT.';
  var CW2_ORDER_FORT = '.CW2_ORDER_FORT.';
  var CW2_ORDER_ARMY = '.CW2_ORDER_ARMY.';
  var CW2_ORDER_GARRISON = '.CW2_ORDER_GARRISON.';
  var CW2_ORDER_PLANET = '.CW2_ORDER_PLANET.';
  
  var CW2_ORDER_MAIN = '.CW2_ORDER_MAIN.';
  var CW2_ORDER_CREATE = '.CW2_ORDER_CREATE.';
  var CW2_ORDER_RECRUIT = '.CW2_ORDER_RECRUIT.';
  var CW2_ORDER_MOVE = '.CW2_ORDER_MOVE.';
  var CW2_ORDER_DESTROY = '.CW2_ORDER_DESTROY.';
  var CW2_ORDER_CHANGE_LOCATION = '.CW2_ORDER_CHANGE_LOCATION.';
  
  var CW2_FORT_COST = '.CW2_FORT_COST.';
  var CW2_MIN_UNIT_COST = '.CW2_MIN_UNIT_COST.';
  var CW2_GLICKO_UNIT_COST = '.CW2_GLICKO_UNIT_COST.';
  var CW2_COUNT_UNIT_COST = '.CW2_COUNT_UNIT_COST.';

  var Lang_cw2_order_apply = "'.jst($GLOBALS['lang_cw2_order_apply']).'";
  var Lang_cw2_order_cancel = "'.jst($GLOBALS['lang_cw2_order_cancel']).'";
 </script>
 <script type="text/javascript" src="cw2_orders.js"></script>
 <script type="text/javascript">
 
';
  $count = 0;
  foreach ($order_types as $order_id => &$order)
  {
   if (IsSet($order['destination']) && ($order['destination'])) {$has_destination = 1;} else {$has_destination = 0;}
   if (IsSet($order['non_neutral']) && ($order['non_neutral'])) {$non_neutral = 1;} else {$non_neutral = 0;}
   if (IsSet($order['tgroup']) && ($order['tgroup'])) {$has_tgroup = 1;} else {$has_tgroup = 0;}
   $tplayer_permissions = 0;
   if (IsSet($order['tplayer_permissions']))
   {
    $tplayer_permissions = $order['tplayer_permissions'];
   }
   if (IsSet($GLOBALS['lang_cw2_orders'][$order_id]))
   {
    $txt = $GLOBALS['lang_cw2_orders'][$order_id];
   }
   else
   {
    $txt = $GLOBALS['lang_cw2_orders'][$order_id & CW2_ORDER_MAIN];
   }
   echo '
 Order_types['.$count.'] = new C_Order_Type('.$order_id.', "'.jst($txt).'", '.$order['order_permissions'].', '.$tplayer_permissions.', '.$order['align'].', '.$has_destination.', '.$non_neutral.', '.$has_tgroup.');';
   $order['js_id'] = $count;
   $count++;
  }

  $count = 0;
  foreach ($tgroups as $tgroup_id => &$tgroup)
  {
   $tgroup_color = "#" . ColorToHex($tgroup['color']);
   echo '
 TGroups['.$count.'] = new C_TGroup('.$tgroup_id.', "'.jst($tgroup['name']).'", "'.$tgroup_color.'", '.$tgroup['credits'].');';
   $tgroup['js_id'] = $count;
   $count++;
  }

 echo '
 var Tournament = new C_Tournament('.$tgroups[$tournament['neutral_tgroup_id']]['js_id'].');';
  
  $count = 0;
  foreach ($planets as $planet_id => &$planet)
  {
   $planet['js_id'] = $count;
   $count++;
  }
  foreach ($planets as $planet_id => &$planet)
  {
   $starlane_txt = "new Array(";
   for ($i = 0; $i < CW2_MAX_STARLANES; $i++)
   {
    if ($i)
    {
     $starlane_txt .= ", ";
    }
    $starlane_id = $planet['starlane'.$i];
    if (IsSet($planets[$starlane_id]))
    {
     $starlane_jsid = $planets[$starlane_id]['js_id'];
    }
    else
    {
     $starlane_jsid = CW2_NO_OBJECT;
    }
    $starlane_txt .= $starlane_jsid;
   }
   $starlane_txt .= ")";
   echo '
 Planets['.$planet['js_id'].'] = new C_Planet('.$planet_id.', "'.jst($planet['planet_name']).'", '.$planet['planet_flags'].', '.$tgroups[$planet['tgroup_id']]['js_id'].', "'.$planet['tgroup_color'].'", '.$starlane_txt.');';
  }

  $count = 0;
  foreach ($tplayers as $tplayer_id => &$tplayer)
  {
   echo '
 TPlayers['.$count.'] = new C_TPlayer('.$tplayer_id.', "'.jst($tplayer['name']).'", '.$tgroups[$tplayer['tgroup_id']]['js_id'].', '.$tplayer['cw2_player_flags'].', '.$tplayer['glicko'].');';
   $tplayer['js_id'] = $count;
   $count++;
  }
  
  $count = 0;
  foreach ($units as $unit_id => &$unit)
  {
   echo '
 Units['.$count.'] = new C_Unit('.$unit_id.', '.$tplayers[$unit['tplayer_id']]['js_id'].', '.$tgroups[$unit['tgroup_id']]['js_id'].', '.$planets[$unit['planet_id']]['js_id'].', '.$unit['unit_flags'].');';
   $unit['js_id'] = $count;
   $count++;
  }

  $count = 0;
  foreach ($orders as $order_id => &$order)
  {
   if (CW2_IsCanViewOrder($order['order_type'], $order['tgroup_id']))
   {
    $unit_jsid = CW2_NO_OBJECT;
    if ($order['unit_id'] > 0)
    {
     $unit_jsid = $units[$order['unit_id']]['js_id'];
    }
    $destination_jsid = CW2_NO_OBJECT;
    if ($order['destination_id'] > 0)
    {
     $destination_jsid = $planets[$order['destination_id']]['js_id'];
    }
    echo '
 Orders['.$count.'] = new C_Order('.CW2_NO_OBJECT.', '.$count.', '.$order_id.', '.$order_types[$order['order_type']]['js_id'].', '.$order['tournament_id'].', '.$order['turn'].', '.$destination_jsid.', '.$tplayers[$order['tplayer_id']]['js_id'].', '.$tgroups[$order['tgroup_id']]['js_id'].', '.$unit_jsid.', "'.jst($order['comment']).'");';
    $order['js_id'] = $count;
    $count++;
   }
  }

  $count = 0;
  foreach ($GLOBALS['lang_cw2_permissions'] as $permission_id => $permission_name)
  {
   echo '
 Permission_types['.$count.'] = new C_Permission('.$permission_id.', "'.jst($permission_name).'");';
   $count++;
  }

echo '  
  var Logged_permissions = '.$logged_permissions.';';
  
  if (IsSet($tgroups[$logged_tgroup_id]))
  {
   echo '
  var Logged_tgroup_jsid = '.$tgroups[$logged_tgroup_id]['js_id'].';';
  }
  else
  {
   echo '
  var Logged_tgroup_jsid = CW2_NO_OBJECT;';
  }
  if (IsSet($tplayers[$logged_tplayer_id]))
  {
   echo '
  var Logged_tplayer_jsid = '.$tplayers[$logged_tplayer_id]['js_id'].';';
  }
  else
  {
   echo '
  var Logged_tplayer_jsid = CW2_NO_OBJECT;';
  }
echo '  
  var Logged_player_id = '.$logged_player_id.';
';
  
 echo '
 </script>
 ';
 
 echo '<style>body {background-color:black; color:white; margin:0; font-family:arial; font-size:10pt} font {font-family:arial; font-size:10pt;}</style> <body onload="doOnLoad()">';
 if (CW2_DEBUG)
 {
  echo '
<span id="cw2_log"></span>';
 }
 echo '
<table border=1><tr><td>'.$GLOBALS['lang_cw2_player'].'</td><td>'.$GLOBALS['lang_cw2_clan'].'</td></tr>';
 foreach ($planets as $planet_id=>&$planet)
 {
  //PLANET
  echo '
  <tr id="planet_'.$planet['js_id'].'" OnClick="planetOrdersClick('.$planet['js_id'].');"><td style="background:'.$planet['tgroup_color'].'"><a href="./systembattle.php?tournament_id=' . $tournament_id . '&planet_id=' . $planet['planet_id'] . '" target="viewwindow"><b>'.$planet['planet_name'].'</b></a>
<div title="'.$planet['map_name'].'">';
  if ($planet['map_icon_url'] != "")
  {
   echo '<img src="'.$planet['map_icon_url'].'" width="20" height="20">';
  }
  else
  {
   echo $planet['map_name'];
  }
  echo '</div></td>';
  EchoClanTD_cl($planet['tgroup_color'], $planet['tgroup_logo_url']);
  echo '</tr>';
  
  //FORT
  if ($planet['planet_flags'] & CW2_PLANET_FORT)
  {
   EchoFortTR_cl($planet['tgroup_color'], $planet['tgroup_logo_url'], $planet['js_id']);
  }
  
  //UNITS
  foreach ($planet['unit_groups'] as $unit_group_id)
  {
   $unit_group = $unit_groups[$unit_group_id];
   foreach ($unit_group['units'] as $unit_id)
   {
    EchoUnitTR_id($unit_id);
   }
  }
 }
 
 echo '
</body></html>';
}

//CONST

$planet_icon_radius = 60;
$planet_icon_type = 1;
$fort_icon_radius = 50;
$fort_icon_type = 2;
$unit_group_icon_radius = 50;
$unit_group_icon_type = 3;
$min_radius = 30;


//MAIN

$tournament_id = 0;
if (IsSet($_GET['tournament_id']))
{
 $tournament_id = IntVal($_GET['tournament_id']);
}

$logged_player_id = CW2_NO_OBJECT;
$logged_tplayer_id = CW2_NO_OBJECT;
$logged_tgroup_id = CW2_NO_OBJECT;
$logged_permissions = 0;
if (IsSet($_GET['player_id']))
{
 $logged_player_id = IntVal($_GET['player_id']);
}

$dbl=DBConn();

if (IsSet($_GET['income']))
{
 echo '<html><body>
';
}
else if (IsSet($_GET['key']))
{
 ShowKey($tournament_id);
}
else if (IsSet($_GET['map']))
{
 ShowMap($tournament_id);
}
else
{
 ?>
 <html>

 <script language="JScript">
 if(document.all) // IE check
   window.defaultStatus = "Press F11 to view map in Full Screen mode.";
   
   Graphics g = getGraphics();
   g.setColor(Color.yellow); g.drawLine(1, 1, 20, 20);
 </script>


 <frameset rows='47,*'>
   <frame src='./cw2.php?tournament_id=<?php echo $tournament_id;?>&income=1'>
   <frameset cols='80%,20%'>
     <frame src='./cw2.php?tournament_id=<?php echo $tournament_id;?>&map=1 name='viewwindow'>
     <frame src='./cw2.php?tournament_id=<?php echo $tournament_id;?>&key=1'>

   </frameset>
 </frameset>
 </html>
 <?php
}

?>

</body>
</html>
